<?php
/************************************************************/
/*			 		Class Mère Model 						*/
/************************************************************/
//Toutes les classes de controle doivent hériter de cette classe

class Model{
public $table;/*nom de la table dans la base de données*/
	public $id;
	public $rules;
	public $form;
	private $databaseSelected;

	/*ouverture de la connexiona la base de données*/ 
	function __construct(){
		require_once( ROOT. 'config/dataBaseConnect.php');
		$this->databaseSelected = new dataBaseConnect();
	}


	/*public function read($fields=null){
		//$this->databaseSelected->open();
		if($fields==null){$fields='*';}
			$sql = "SELECT $fields FROM ".$this->table." WHERE ".$this->id."=".$this->id;
			$req = mysql_query($sql) or die(mysql_error()."<br/> => ".mysql_query());
			$data = mysql_fetch_assoc($req);
		foreach($data as $k=>$v){
			$this->$k = $v;
		}
		//$this->databaseSelected->close();
	}*/

	/*ajouter ou modifier une donnée dans la table*/
	public function save($data){
		//update
		if(isset($data[$this->id]) && !empty($data[$this->id])){
			$sql = "UPDATE ".$this->table." SET ";
			foreach($data as $k=>$v){
					$sql .= $k.'= '.$v.' ,';
			}
			$sql = substr($sql, 0, -1);
			$sql .= " WHERE ".$this->id."=".$data[$this->id];
			//echo '<br/>'.$sql;
		}else{
		//insert
			$sql = "INSERT INTO ".$this->table."(";
			foreach($data as $k=>$v){
				$sql .=" $k,";
			} 
			$sql = substr($sql, 0, -1);
			$sql .= ") VALUES (";
			foreach($data as $k=>$v){
				$sql .=" '$v',";
			}
			$sql = substr($sql, 0, -1);
			$sql .= ")";
			//echo '<br/>'.$sql;
		}
		$req = $this->request($sql);
	}

	/*Rquete select*/
	public function find($data=array()){
	//$this->databaseSelected->open();
		$conditions = "1=1";
		$fields = "*";
		$limit = "";
		$order = $this->id." DESC ";
		if(isset($data['conditions'])){ $conditions=$data['conditions']; }
		if(isset($data['fields'])){ $fields=$data['fields']; }
		if(isset($data['limit'])){ $limit=$data['limit']; }
		if(isset($data['order'])){ $order = $data["order"]; }	
		$sql = "SELECT ".$fields." FROM ".$this->table." WHERE $conditions ORDER BY $order $limit ";
		$d = array();
		$req = $this->request($sql);
		return $req;
	}
	
	public function delete($id){
		//if($id==null){ $id = $this->id; }
		$sql = "DELETE FROM ".$this->table." WHERE ".$this->id."=".$id;
		$this->request($sql);
	}

	public function request($sql){
	$d = array();
		try{
			$request = $this->databaseSelected->getDataBase()->prepare($sql);
			$request->execute();
			while($row = $request->fetch(PDO::FETCH_ASSOC)){
				$d[] = $row; 
			}
			return $d;
		}catch (Exception $e){
			echo "Une erreur s'est produite lors de l'execution de la requete";
		}
	}

	static function load($name){
		require("$name.php");
	}
	public function exists($vField, $field){
		$existe = false;
		//echo $field;
		//echo $vField;
		$result = $this->find(array(
									'conditions' => $field."='".$vField."'",
								));
		if(count($result) != 0){
			$existe = true;
		}
		return $existe;
	}//--->fin exists()
}
?>